package com.niit.project.controller;

import com.niit.project.entity.User;
import com.niit.project.service.UserService;
import com.niit.project.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.*;
import javax.servlet.http.*;
import java.io.IOException;

//张笑玉——用户登录
@WebServlet(name = "UserLoginController", value = "/UserLoginController")
public class UserLoginController extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //接收前端传来的数据
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        UserService userService = new UserServiceImpl();
        //调用login方法，进行登录
        User user = userService.login(username,password);
        if(user!=null)
        {
            HttpSession session = request.getSession();
            session.setAttribute("user",user);
            //重定向
            response.sendRedirect(request.getContextPath()+"/UserGoodsListController");

        }else {
            System.out.println("登陆失败");
        }
    }
}
